home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / RGASM.RAR / ASMCODE.EXE / CHAPT1-5 / NUMDIR.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-08-29  |  2.8 KB  |  54 lines

  1. ;
  2. ;       Program NumDir
  3. ;
  4. .model  small                   ; small memory model - one segment only
  5. .stack                          ; stack of default size
  6. .data                           ; this starts data segment
  7. CR      equ     0Dh             ; Carriage Return
  8. LF      equ     0Ah             ; Line Feed
  9. EndMsg  equ     '$'             ; End of message
  10. MsgIn   db      CR,LF,'Enter a number less than 65537: ',EndMsg
  11. MsgOut  db      CR,LF,'You have entered the number ',EndMsg
  12. UnsWord dw      0               ; number to be output
  13. Ten     dw      10              ; constant for obtaining decimal digits
  14. .code                           ; this starts code segments
  15. .startup                        ; this initializes segment registers
  16.     mov     ah,09h          ; function 09h - output text string
  17.     lea     dx,MsgIn        ; address of message into DX
  18.     int     21h             ; DOS service call
  19. NextCh: mov     ah,01           ; function 01h - keyboard input
  20.     int     21h             ; DOS service call
  21.     .IF     al == 0
  22.     int     21h             ; read code of special character
  23.     .ENDIF  
  24. NotSpec:cmp     al,'0'          ; compare character read to "0" (number?)
  25.     jb      OutNum          ; if not, don't process
  26.     cmp     al,'9'          ; compare character read to "9" (number?)
  27.     ja      OutNum          ; if not, don't process 
  28. ProcNum:sub     al,'0'          ; convert character to number
  29.     mov     bl,al           ; copy character read into BX
  30.     mov     ax,UnsWord      ; hex number into AX
  31.     mul     Ten             ; one decimal position to the left
  32.     mov     UnsWord,ax      ; store result back into memory
  33.     add     UnsWord,bx      ; add current hex digit
  34.     jmp     NextCh          ; read next character
  35. OutNum: mov     ah,09h          ; function 09h - output text string
  36.     lea     dx,MsgOut       ; address of message into DX
  37.     int     21h             ; DOS service call
  38.     mov     ax,UnsWord      ; place number to be printed into AX.
  39.     mov     cx,0            ; clear counter of digits 
  40. NexDiv: mov     dx,0            ; clear high part of number
  41.     div     Ten             ; divide number to be prinred by 10
  42.     push    dx              ; push remainder (current digit) into stack.
  43.     inc     cx              ; increase counter of digits
  44.     cmp     ax,0            ; result is zero? (number was less than 10)
  45.     jne     NexDiv          ; if not, continue process (get next digit)
  46.     mov     ax,0200h        ; function 02h - output character
  47. OutSym: pop     dx              ; pop current digit from stack
  48.     add     dl,30h          ; convert digit to character
  49.     int     21h             ; DOS service call
  50.     loop    OutSym          ; to output next digit
  51. FinProg:mov     ax,4C00h        ; function 4Ch - terminate process (exit code 0)
  52.     int     21h             ; DOS service call
  53.     end     
  54.